"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const LogTool_1 = __importDefault(require("../com/LogTool/LogTool"));
class HttpUtil {
    baseUrl;
    constructor(baseUrl) {
        this.baseUrl = baseUrl || '';
    }
    async getJson(endpoint, params = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            url.search = new URLSearchParams(params).toString();
            const response = await fetch(url, {
                method: 'GET',
                headers: {
                    ...headers
                }
            });
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return await response.json();
        }
        catch (err) {
            const logger = await LogTool_1.default.getLogger();
            logger.warn('HttpUtil GET', { message: err + err.stack });
        }
    }
    async post(endpoint, body = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'content-type': 'application/json',
                    ...headers
                },
                body: JSON.stringify(body)
            });
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return await response.json();
        }
        catch (err) {
            const logger = await LogTool_1.default.getLogger();
            logger.warn('HttpUtil POST', { message: err + err.stack });
        }
    }
    async put(endpoint, body = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            const response = await fetch(url, {
                method: 'PUT',
                headers: {
                    'content-type': 'application/json',
                    ...headers
                },
                body: JSON.stringify(body)
            });
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return await response.json();
        }
        catch (err) {
            const logger = await LogTool_1.default.getLogger();
            logger.warn('HttpUtil PUT', { message: err + err.stack });
        }
    }
    async delete(endpoint, params = {}, headers = {}) {
        try {
            const url = new URL(endpoint, this.baseUrl);
            url.search = new URLSearchParams(params).toString();
            const response = await fetch(url, {
                method: 'DELETE',
                headers: {
                    ...headers
                }
            });
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return await response.json();
        }
        catch (err) {
            const logger = await LogTool_1.default.getLogger();
            logger.warn('HttpUtil DELETE', { message: err + err.stack });
        }
    }
}
exports.default = HttpUtil;
